# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
from openpyxl import Workbook

class EscPipeline:
    def __init__(self):
        self.wb = Workbook()
        self.ws = self.wb.active
        self.ws.append(['型号', '信息', '新车价格', '当前价格'])

        # self.f = open("./二手车1.csv", mode="a", encoding="utf-8")
        # self.f.append(['型号', '信息', '当前价格', '新车价格'])

    def open_spider(self, spider):
        pass

    def close_spider(self, spider):
        # if self.ws:
        #     self.ws.close()
        pass

    def process_item(self, item, spider):
        line = [item['title'],item['msg'],item['newpri'],item['nowpri']]
        self.ws.append(line)
        self.wb.save('别克二手车.xlsx')
        # txt = str.format('{},{},{},{}\n',item['title'],item['msg'],item['newpri'],item['nowpri'])
        # self.ws.write(txt)
        return item
